<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="预约人id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="预约人id"></el-input>
    </el-form-item>
    <el-form-item label="预约座位id" prop="seatId">
      <el-input v-model="dataForm.seatId" placeholder="预约座位id"></el-input>
    </el-form-item>
    <el-form-item label="座位编号" prop="seatName">
      <el-input v-model="dataForm.seatName" placeholder="座位编号"></el-input>
    </el-form-item>
    <el-form-item label="预约开始时间" prop="time">
      <el-input v-model="dataForm.time" placeholder="预约开始时间"></el-input>
    </el-form-item>
    <el-form-item label="截止时间" prop="overTime">
      <el-input v-model="dataForm.overTime" placeholder="截止时间"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="修改人" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="修改人"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createDate">
      <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateDate">
      <el-input v-model="dataForm.updateDate" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="状态(0:未签到 1:签到 2:违约)" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态(0:未签到 1:签到 2:违约)"></el-input>
    </el-form-item>
    <el-form-item label="是否删除" prop="isDelete">
      <el-input v-model="dataForm.isDelete" placeholder="是否删除"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          seatId: '',
          seatName: '',
          time: '',
          overTime: '',
          createBy: '',
          updateBy: '',
          createDate: '',
          updateDate: '',
          status: '',
          isDelete: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '预约人id不能为空', trigger: 'blur' }
          ],
          seatId: [
            { required: true, message: '预约座位id不能为空', trigger: 'blur' }
          ],
          seatName: [
            { required: true, message: '座位编号不能为空', trigger: 'blur' }
          ],
          time: [
            { required: true, message: '预约开始时间不能为空', trigger: 'blur' }
          ],
          overTime: [
            { required: true, message: '截止时间不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ],
          createDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateDate: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态(0:未签到 1:签到 2:违约)不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '是否删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/seat/appoint/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.appoint.userId
                this.dataForm.seatId = data.appoint.seatId
                this.dataForm.seatName = data.appoint.seatName
                this.dataForm.time = data.appoint.time
                this.dataForm.overTime = data.appoint.overTime
                this.dataForm.createBy = data.appoint.createBy
                this.dataForm.updateBy = data.appoint.updateBy
                this.dataForm.createDate = data.appoint.createDate
                this.dataForm.updateDate = data.appoint.updateDate
                this.dataForm.status = data.appoint.status
                this.dataForm.isDelete = data.appoint.isDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/seat/appoint/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'seatId': this.dataForm.seatId,
                'seatName': this.dataForm.seatName,
                'time': this.dataForm.time,
                'overTime': this.dataForm.overTime,
                'createBy': this.dataForm.createBy,
                'updateBy': this.dataForm.updateBy,
                'createDate': this.dataForm.createDate,
                'updateDate': this.dataForm.updateDate,
                'status': this.dataForm.status,
                'isDelete': this.dataForm.isDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
